package com.sophialex.health.record.common.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.swt.graphics.RGB;

import com.sophialex.health.system.frame.util.Assert;
import com.sophialex.health.system.frame.util.NumberUtil;

public class DataToString {

	public static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
	public final static String flsdj = "flsdj";
	public final static String fjhdj = "fjhdj";
	public final static String fpfdj = "fpfdj";
	public final static String fje = "fje";

	public static String toString(Object obj, String field) {
		if (obj == null)
			return "";
		StringBuffer buff = new StringBuffer("");
		if (obj instanceof String)
			return (String) obj;
		if (obj instanceof Long)
			return ((Long) obj).toString();
		if (obj instanceof Integer)
			return ((Integer) obj).toString();
		if (obj instanceof Double)
			return DoubleToString(obj, field).replace(",", "");
		if (obj instanceof Date)
			return sf.format((Date) obj);
		if (obj instanceof Timestamp)
			return sf.format((Timestamp) obj);
		if (obj instanceof RGB) {
			RGB rgb = (RGB) obj;
			buff.append(rgb.red).append(",").append(rgb.green).append(",")
					.append(rgb.blue);
			return buff.toString();
		}
		return "";
	}

	/**
	 * 转换Double型,自动去掉多除的0,.
	 * 
	 * @param obj
	 * @param field
	 * @return
	 */
	public static String DoubleToSimpString(Object obj, String field) {
		int pos = 2;
		if (field.equals("flsdj") || field.equals("fzhlsdj")) {
			String str = ParamUtil.getParamValue(ParamUtil.LSDJPOS);
			if (!Assert.isNull(str)) {
				pos = Integer.parseInt(str);
			}
		} else if (field.equals("fpfdj") || field.equals("fzhpfdj")) {
			String str = ParamUtil.getParamValue(ParamUtil.PFDJPOS);
			if (!Assert.isNull(str)) {
				pos = Integer.parseInt(str);
			}
		} else if (field.equals("fjhdj") || field.equals("fzhjhdj")) {
			String str = ParamUtil.getParamValue(ParamUtil.JHDJPOS);
			if (!Assert.isNull(str)) {
				pos = Integer.parseInt(str);
			}
		} else if (field.equals("fjhje") || field.equals("fjxce")
				|| field.equals("flsje") || field.equals("frlje")
				|| field.equals(fje)) {
			String str = ParamUtil.getParamValue(ParamUtil.JEXSPOS);
			if (!Assert.isNull(str)) {
				pos = Integer.parseInt(str);
			}
		} else {
			String str = ParamUtil.getParamValue(ParamUtil.JEXSPOS);
			if (!Assert.isNull(str)) {
				pos = Integer.parseInt(str);
			}
		}
		return NumberUtil.getSimFormatValue((Double) obj, pos);

	}

	/**
	 * 转换Double型,自动补0
	 * 
	 * @param obj
	 * @param field
	 * @return
	 */
	public static String DoubleToString(Object obj, String field) {
		int pos = 4;
		if (field.equals("flsdj") || field.equals("fzhlsdj")) {
			String str = ParamUtil.getParamValue(ParamUtil.LSDJPOS);
			if (!Assert.isNull(str)) {
				pos = Integer.parseInt(str);
			}
		} else if (field.equals("fpfdj") || field.equals("fzhpfdj")) {
			String str = ParamUtil.getParamValue(ParamUtil.PFDJPOS);
			if (!Assert.isNull(str)) {
				pos = Integer.parseInt(str);
			}
		} else if (field.equals("fjhdj") || field.equals("fzhjhdj")) {
			String str = ParamUtil.getParamValue(ParamUtil.JHDJPOS);
			if (!Assert.isNull(str)) {
				pos = Integer.parseInt(str);
			}
		} else if (field.equals("fjhje") || field.equals("fjxce")
				|| field.equals("flsje") || field.equals("frlje")
				|| field.equals(fje)) {
			String str = ParamUtil.getParamValue(ParamUtil.JEXSPOS);
			if (!Assert.isNull(str)) {
				pos = Integer.parseInt(str);
			}
		} else {
			String str = ParamUtil.getParamValue(ParamUtil.JEXSPOS);
			if (!Assert.isNull(str)) {
				pos = Integer.parseInt(str);
			}
		}
		return NumberUtil.getFormatValue((Double) obj, pos);
		// return NumberUtil.getSimFormatValue((Double) obj, pos);
	}
}
